package br.com.rocks.rs232;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import br.com.rocks.bluetoothservice.BluetoothService;
import br.com.rocks.bluetoothservice.DadosBluetoothHandler;
import br.com.rocks.bluetoothservice.IDadosBluetoothHandler;

public class Rs232Activity extends Activity implements IDadosBluetoothHandler {

	private BluetoothService bluetoothService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.layout_rs232);

		bluetoothService = new BluetoothService(this, new DadosBluetoothHandler(this));

		configurarAdapterListViewRxText();

		acaoBotaoEnviar();
	}

	private void configurarAdapterListViewRxText() {
		ListView rxText = (ListView) findViewById(R.id.rxText);

		if (rxText.getAdapter() == null) {
			rxText.setAdapter(new ArrayAdapterBalao(this));
		}
	}

	private void acaoBotaoEnviar() {
		((ImageView) findViewById(R.id.btnEnviar)).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				EditText editText = (EditText) findViewById(R.id.txText);
				String msg = editText.getText().toString();
				if (msg.isEmpty()) {
					return;
				}
				bluetoothService.enviar(msg);
				editText.getText().clear();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.global, menu);

		menu.findItem(R.id.action_procurar_dispositivos);
		menu.findItem(R.id.action_esperar_conexao);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.action_procurar_dispositivos: {
			Intent intent = new Intent(this, ListaDeDispositivosActivity.class);
			intent.putExtra(BluetoothDevice.EXTRA_DEVICE, bluetoothService.getBluetoothDevice());
			startActivityForResult(intent, BluetoothAdapter.SCAN_MODE_NONE);
			break;
		}
		case R.id.action_esperar_conexao: {
			Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
			intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, BluetoothService.TEMPO_LIMITE_SERVER_SOCKET);
			startActivityForResult(intent, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
			break;
		}
		}

		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE && resultCode == BluetoothService.TEMPO_LIMITE_SERVER_SOCKET) {
			bluetoothService.esperarConexao(BluetoothService.MY_UUID, resultCode, BluetoothService.INSECURE);
		}
		if (requestCode == BluetoothAdapter.SCAN_MODE_NONE && resultCode == RESULT_OK) {
			BluetoothDevice bluetoothDevice = (BluetoothDevice) data.getExtras().get(BluetoothDevice.EXTRA_DEVICE);
			bluetoothService.conectar(bluetoothDevice, BluetoothService.MY_UUID, BluetoothService.INSECURE);
		}
		if (requestCode == BluetoothAdapter.SCAN_MODE_NONE && resultCode == ListaDeDispositivosActivity.RESULT_DISCONNECTING_DEVICE) {
			bluetoothService.desconectar();
		}
	}

	@Override
	protected void onDestroy() {
		bluetoothService.onDestroy();

		super.onDestroy();
	}

	@Override
	public void estadoDaConexao(String msg) {
		setTitle(msg);
	}

	@Override
	public void mensagemToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}

	@Override
	public void mensagemEnviada(String msg) {
		criarBalaoMensagem(msg, false);
	}

	@Override
	public void mensagemRecebida(String msg) {
		criarBalaoMensagem(msg, true);
	}

	private void criarBalaoMensagem(String msg, boolean isRecebida) {
		ListView rxText = (ListView) findViewById(R.id.rxText);
		((ArrayAdapterBalao) rxText.getAdapter()).add(new DadosDaConversa(msg, isRecebida));
	}
}
